/**
 * 
 * @author Wagner Pinheiro - EGC-NA5
 * Unifieo, Maio de 2011
 * 
 * TÓPICOS AVANÇADOS DE PROGRAMAÇÃO
 * Prof. Imperatore
 * 
 * Exercicio: gerar uma calculadora simples utilizando o pacote Swing 
 * 
 * Fonte de pesquisa:
 * http://paulohiga.com/posts/calculadora-java-swing.php
 * http://www.dreamincode.net/forums/topic/91377-swing-calculator/
 * http://www.javabeginner.com/java-swing/java-swing-calculator
 * http://stackoverflow.com/questions/1667060/can-i-use-switch-case-in-actionperformed-method-in-java
 * http://viralpatel.net/blogs/2008/12/create-jar-file-in-java-eclipse.html
 * http://download.oracle.com/javase/tutorial/deployment/jar/build.html
 * 
 * IDE: Eclipse Galileo
 * Distribuido sobre a licença BSD (ver ultimas linhas do código)
 * Site: www.lunatic.com.br
 * 
 */

package calculadora;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
 * Classe principal da calculadora com definição da interface
 * @author wagner
 *
 */
public class Calculadora extends JFrame implements ActionListener {

    /**
	 * 
	 */
	private static final long serialVersionUID = -3079914396742111042L;
	private static final int height = 240;
    private static final int width = 240;
	private JTextField visor;
	private static String parcial = "";
	private static String total = "";
	private static String op = "";

    public Calculadora() {    	
    	super("Calculadora");
    	setSize(width, height);
    	
    	String labelButtons[] = {"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", "C", "=", "+"};
    	JPanel cp = (JPanel) this.getContentPane();
    	cp.setLayout(new BorderLayout());
    	JPanel p = new JPanel();
    	p.setLayout(new GridLayout(0, 4));
    	for (int i = 0; i < labelButtons.length; ++i) {
    	        JButton button = new JButton(labelButtons[i]);
    	        button.addActionListener(this);
    	        p.add(button);
    	}
    	visor = new JTextField();
    	visor.setHorizontalAlignment(JTextField.RIGHT);
    	visor.setText("0");
    	cp.add(visor, BorderLayout.PAGE_START);
    	cp.add(p, BorderLayout.CENTER);
    	setVisible(true);
    }

    /**
     * metodo principal para inicio do programa
     * @param args
     */
    public static void main(String[] args) {
        //Calculadora calc = new Calculadora();
    	new Calculadora();
    }

    /**
     * implementação dos eventos a serem tratados pela interface 
     * @todo Implementar operações com mais de 2 fafores. 
     * Exemplos de operações possiveis atualmente:
     * 1+1 = 2
     * 2+2 = 4; + 2 = 6; * 3 = 18 ...
     * Alterar para poder fazer: 1 + 1 + 1 +1 = 4
     *   
     */
    public void actionPerformed(ActionEvent e) {   
    	
        //o ideal seria ter um switch, mas o ActionEvent não permite. Ver: http://stackoverflow.com/questions/1667060/can-i-use-switch-case-in-actionperformed-method-in-java
    	if(e.getActionCommand().equals("0")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();
    	}else if(e.getActionCommand().equals("1")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();
    	}else if(e.getActionCommand().equals("2")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();	
	    }else if(e.getActionCommand().equals("3")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();	
	    }else if(e.getActionCommand().equals("4")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();	
	    }else if(e.getActionCommand().equals("5")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();			
	    }else if(e.getActionCommand().equals("6")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();	
	    }else if(e.getActionCommand().equals("7")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();					
		} else if(e.getActionCommand().equals("8")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();			
		} else if(e.getActionCommand().equals("9")){
			visor.setText(parcial + e.getActionCommand());
			parcial = visor.getText();
		} else if(e.getActionCommand().equals("C")){
			visor.setText("0");
			parcial = "";
			total = "";
		} else if(e.getActionCommand().equals("+")){
			visor.setText("0");
			total = parcial;
			parcial = "";
			op = "+";
		} else if(e.getActionCommand().equals("-")){
			visor.setText("0");
			total = parcial;
			parcial = "";
			op = "-";
		} else if(e.getActionCommand().equals("*")){
			visor.setText("0");
			total = parcial;
			parcial = "";
			op = "*";
		} else if(e.getActionCommand().equals("/")){
			visor.setText("0");
			total = parcial;
			parcial = "";
			op = "/";
		
		} else if(e.getActionCommand().equals("=")){
			if(op=="+"){
				total = String.valueOf(Float.parseFloat(total) + Float.parseFloat(parcial));
			}else if(op=="-"){
				total = String.valueOf(Float.parseFloat(total) - Float.parseFloat(parcial));
			}else if(op=="*"){
				total = String.valueOf(Float.parseFloat(total) * Float.parseFloat(parcial));
			}else if(op=="/"){
				total = String.valueOf(Float.parseFloat(total) / Float.parseFloat(parcial));
			}
			parcial = total;
			op = "";
			visor.setText(total);			
		}
        
    
    }
}

/*
Copyright (c) 2011, Wagner Silva Pinheiro
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, 
this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation and/or 
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
OF THE POSSIBILITY OF SUCH DAMAGE.
*/
